package com.kuang.demo01;

import java.util.concurrent.*;

/**多线程实现方法三
 * @author FHL
 * 此方式的好处：
 *  可定义返回值、可输出异常
 */
public class TestCallable implements Callable<Boolean> {
    //网络图片地址
    private String url;
    //保存的文件名
    private String name;
    private TestCallable(String url, String name) {
        this.url=url;
        this.name=name;
    }
    //下载图片的执行体
    @Override
    public Boolean call() throws Exception {
WebDownLoad webDownLoad=new WebDownLoad();
webDownLoad.downLoad(url,name);
        System.out.println("下载了名为"+name+"的图片！");
        return true;
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
TestCallable t1=new TestCallable("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1608528959607&di=8df4f714962af07aea406a8d17a973a2&imgtype=0&src=http%3A%2F%2Fa3.att.hudong.com%2F25%2F08%2F01300000248343122451081855461.jpg","1.jpg");
TestCallable t2=new TestCallable("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1608528959607&di=56dc1022c40f76c1065d637875c8daff&imgtype=0&src=http%3A%2F%2Fa3.att.hudong.com%2F35%2F34%2F19300001295750130986345801104.jpg","2.jpg");
TestCallable t3=new TestCallable("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1608528959607&di=e662fc8d87ee2067fa365ee3083ddc53&imgtype=0&src=http%3A%2F%2Fa2.att.hudong.com%2F42%2F31%2F01300001320894132989315766618.jpg","3.jpg");
        //创建执行服务
        ExecutorService ser = Executors.newFixedThreadPool(3);
        //提交执行
        Future<Boolean> r1=ser.submit(t1);
        Future<Boolean> r2 = ser.submit(t2);
        Future<Boolean> r3 = ser.submit(t3);
        //获取结果
        Boolean rs1 = r1.get();
        Boolean rs2 = r2.get();
        Boolean rs3 = r3.get();
        //关闭服务
        ser.shutdownNow();
    }
}
